package com.intelligent.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.intelligent.dto.PurchaseDemandAddDTO;
import com.intelligent.dto.PurchaseDemandQueryDTO;
import com.intelligent.service.PurchaseDemandService;
import com.intelligent.utils.Result;
import com.intelligent.vo.PurchaseDemandVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 采购需求管理Controller
 */
@RestController
@RequestMapping("/purchase/demand")
public class PurchaseDemandController {

    @Autowired
    private PurchaseDemandService purchaseDemandService;

    /**
     * 分页查询采购需求列表
     * 支持按采购需求单号模糊查询、采购状态筛选等功能
     * 
     * @param queryDTO 查询条件
     * @return 分页结果
     */
    @PostMapping("/page")
    public Result<IPage<PurchaseDemandVO>> page(@RequestBody PurchaseDemandQueryDTO queryDTO) {
        try {
            IPage<PurchaseDemandVO> result = purchaseDemandService.pageList(queryDTO);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error("查询失败: " + e.getMessage());
        }
    }

    /**
     * 新增采购需求
     * 
     * @param addDTO 新增数据
     * @return 操作结果
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody PurchaseDemandAddDTO addDTO) {
        try {
            boolean ok = purchaseDemandService.add(addDTO);
            return ok ? Result.success(true) : Result.error("新增失败");
        } catch (IllegalArgumentException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("新增失败: " + e.getMessage());
        }
    }
} 